import java.util.HashMap;
import java.util.SortedSet;
import java.util.TreeSet;


public class TransitionTable {
	private HashMap<State, State>[] cols;
	
	
	/**
	 * 
	 * @param numInputs number of the machine's possible inputs
	 */
	@SuppressWarnings("unchecked")
	public TransitionTable(int numInputs){
		cols = new HashMap[numInputs];
		for(int i = 0; i < cols.length; i++)
			cols[i] = new HashMap<State, State>();
	}	
	
	/**
	 * adds a new row to the transition table
	 * @param fields State array of length N+1, the initial state at index 0,
	 * resulting states at index "input+1"
	 */
	public void addRow(State[] fields){
		if(fields.length == cols.length + 1)
			for(int i = 1; i < fields.length; i++)
				cols[i-1].put(fields[0], fields[i]);
	}
	
	/**
	 * 
	 * @param currentState
	 * @param input
	 * @return next state resulting from the currentState receiving input
	 */
	public State getNextState(State currentState, int input){
		State search = new State(currentState.name);
		State found = cols[input].get(search);
		return new State(found.name, found.output);
	}
	
	/**
	 * @return a SortedSet of machine states
	 */
	public SortedSet<State> getStateSet(){
		return new TreeSet<State>(cols[0].keySet());
	}
}
